


<!DOCTYPE html>
<html id="htmlId">
<head>
  <title>Coverage Report > BoardPanel</title>
  <style type="text/css">
    @import "../../css/coverage.css";
    @import "../../css/highlight-idea.css";
  </style>
  <script type="text/javascript" src="../../js/highlight.pack.js"></script>
</head>

<body>
<div class="content">
<div class="breadCrumbs">
Current scope:     <a href="../../index.html">all classes</a>
    <span class="separator">|</span>
    <a href="../index.html">nl.tudelft.jpacman.ui</a>
</div>

<h1>Coverage Summary for Class: BoardPanel (nl.tudelft.jpacman.ui)</h1>

<table class="coverageStats">
<tr>
  <th class="name">Class</th>
<th class="coverageStat 
">
  Class, %
</th>
<th class="coverageStat 
">
  Method, %
</th>
<th class="coverageStat 
">
  Line, %
</th>
</tr>
<tr>
  <td class="name">BoardPanel</td>
<td class="coverageStat">
  <span class="percent">
    0%
  </span>
  <span class="absValue">
    (0/1)
  </span>
</td>
<td class="coverageStat">
  <span class="percent">
    0%
  </span>
  <span class="absValue">
    (0/5)
  </span>
</td>
<td class="coverageStat">
  <span class="percent">
    0%
  </span>
  <span class="absValue">
    (0/27)
  </span>
</td>
</tr>

</table>

<br/>
<br/>


<pre>
<div class="sourceCode" id="sourceCode"><i class="no-highlight">1</i>&nbsp;package nl.tudelft.jpacman.ui;
<i class="no-highlight">2</i>&nbsp;
<i class="no-highlight">3</i>&nbsp;import java.awt.Color;
<i class="no-highlight">4</i>&nbsp;import java.awt.Dimension;
<i class="no-highlight">5</i>&nbsp;import java.awt.Graphics;
<i class="no-highlight">6</i>&nbsp;
<i class="no-highlight">7</i>&nbsp;import javax.swing.JPanel;
<i class="no-highlight">8</i>&nbsp;
<i class="no-highlight">9</i>&nbsp;import nl.tudelft.jpacman.board.Board;
<i class="no-highlight">10</i>&nbsp;import nl.tudelft.jpacman.board.Square;
<i class="no-highlight">11</i>&nbsp;import nl.tudelft.jpacman.board.Unit;
<i class="no-highlight">12</i>&nbsp;import nl.tudelft.jpacman.game.Game;
<i class="no-highlight">13</i>&nbsp;
<i class="no-highlight">14</i>&nbsp;/**
<i class="no-highlight">15</i>&nbsp; * Panel displaying a game.
<i class="no-highlight">16</i>&nbsp; *
<i class="no-highlight">17</i>&nbsp; * @author Jeroen Roosen 
<i class="no-highlight">18</i>&nbsp; *
<i class="no-highlight">19</i>&nbsp; */
<b class="nc"><i class="no-highlight">20</i>&nbsp;class BoardPanel extends JPanel {</b>
<i class="no-highlight">21</i>&nbsp;
<i class="no-highlight">22</i>&nbsp;    /**
<i class="no-highlight">23</i>&nbsp;     * Default serialisation ID.
<i class="no-highlight">24</i>&nbsp;     */
<i class="no-highlight">25</i>&nbsp;    private static final long serialVersionUID = 1L;
<i class="no-highlight">26</i>&nbsp;
<i class="no-highlight">27</i>&nbsp;    /**
<i class="no-highlight">28</i>&nbsp;     * The background colour of the board.
<i class="no-highlight">29</i>&nbsp;     */
<b class="nc"><i class="no-highlight">30</i>&nbsp;    private static final Color BACKGROUND_COLOR = Color.BLACK;</b>
<i class="no-highlight">31</i>&nbsp;
<i class="no-highlight">32</i>&nbsp;    /**
<i class="no-highlight">33</i>&nbsp;     * The size (in pixels) of a square on the board. The initial size of this
<i class="no-highlight">34</i>&nbsp;     * panel will scale to fit a board with square of this size.
<i class="no-highlight">35</i>&nbsp;     */
<i class="no-highlight">36</i>&nbsp;    private static final int SQUARE_SIZE = 16;
<i class="no-highlight">37</i>&nbsp;
<i class="no-highlight">38</i>&nbsp;    /**
<i class="no-highlight">39</i>&nbsp;     * The game to display.
<i class="no-highlight">40</i>&nbsp;     */
<i class="no-highlight">41</i>&nbsp;    private final Game game;
<i class="no-highlight">42</i>&nbsp;
<i class="no-highlight">43</i>&nbsp;    /**
<i class="no-highlight">44</i>&nbsp;     * Creates a new board panel that will display the provided game.
<i class="no-highlight">45</i>&nbsp;     *
<i class="no-highlight">46</i>&nbsp;     * @param game
<i class="no-highlight">47</i>&nbsp;     *            The game to display.
<i class="no-highlight">48</i>&nbsp;     */
<i class="no-highlight">49</i>&nbsp;    BoardPanel(Game game) {
<b class="nc"><i class="no-highlight">50</i>&nbsp;        super();</b>
<b class="nc"><i class="no-highlight">51</i>&nbsp;        assert game != null;</b>
<b class="nc"><i class="no-highlight">52</i>&nbsp;        this.game = game;</b>
<i class="no-highlight">53</i>&nbsp;
<b class="nc"><i class="no-highlight">54</i>&nbsp;        Board board = game.getLevel().getBoard();</b>
<i class="no-highlight">55</i>&nbsp;
<b class="nc"><i class="no-highlight">56</i>&nbsp;        int w = board.getWidth() * SQUARE_SIZE;</b>
<b class="nc"><i class="no-highlight">57</i>&nbsp;        int h = board.getHeight() * SQUARE_SIZE;</b>
<i class="no-highlight">58</i>&nbsp;
<b class="nc"><i class="no-highlight">59</i>&nbsp;        Dimension size = new Dimension(w, h);</b>
<b class="nc"><i class="no-highlight">60</i>&nbsp;        setMinimumSize(size);</b>
<b class="nc"><i class="no-highlight">61</i>&nbsp;        setPreferredSize(size);</b>
<i class="no-highlight">62</i>&nbsp;    }
<i class="no-highlight">63</i>&nbsp;
<i class="no-highlight">64</i>&nbsp;    @Override
<i class="no-highlight">65</i>&nbsp;    public void paint(Graphics g) {
<b class="nc"><i class="no-highlight">66</i>&nbsp;        assert g != null;</b>
<b class="nc"><i class="no-highlight">67</i>&nbsp;        render(game.getLevel().getBoard(), g, getSize());</b>
<i class="no-highlight">68</i>&nbsp;    }
<i class="no-highlight">69</i>&nbsp;
<i class="no-highlight">70</i>&nbsp;    /**
<i class="no-highlight">71</i>&nbsp;     * Renders the board on the given graphics context to the given dimensions.
<i class="no-highlight">72</i>&nbsp;     *
<i class="no-highlight">73</i>&nbsp;     * @param board
<i class="no-highlight">74</i>&nbsp;     *            The board to render.
<i class="no-highlight">75</i>&nbsp;     * @param graphics
<i class="no-highlight">76</i>&nbsp;     *            The graphics context to draw on.
<i class="no-highlight">77</i>&nbsp;     * @param window
<i class="no-highlight">78</i>&nbsp;     *            The dimensions to scale the rendered board to.
<i class="no-highlight">79</i>&nbsp;     */
<i class="no-highlight">80</i>&nbsp;    private void render(Board board, Graphics graphics, Dimension window) {
<b class="nc"><i class="no-highlight">81</i>&nbsp;        int cellW = window.width / board.getWidth();</b>
<b class="nc"><i class="no-highlight">82</i>&nbsp;        int cellH = window.height / board.getHeight();</b>
<i class="no-highlight">83</i>&nbsp;
<b class="nc"><i class="no-highlight">84</i>&nbsp;        graphics.setColor(BACKGROUND_COLOR);</b>
<b class="nc"><i class="no-highlight">85</i>&nbsp;        graphics.fillRect(0, 0, window.width, window.height);</b>
<i class="no-highlight">86</i>&nbsp;
<b class="nc"><i class="no-highlight">87</i>&nbsp;        for (int y = 0; y &lt; board.getHeight(); y++) {</b>
<b class="nc"><i class="no-highlight">88</i>&nbsp;            for (int x = 0; x &lt; board.getWidth(); x++) {</b>
<b class="nc"><i class="no-highlight">89</i>&nbsp;                int cellX = x * cellW;</b>
<b class="nc"><i class="no-highlight">90</i>&nbsp;                int cellY = y * cellH;</b>
<b class="nc"><i class="no-highlight">91</i>&nbsp;                Square square = board.squareAt(x, y);</b>
<b class="nc"><i class="no-highlight">92</i>&nbsp;                render(square, graphics, cellX, cellY, cellW, cellH);</b>
<i class="no-highlight">93</i>&nbsp;            }
<i class="no-highlight">94</i>&nbsp;        }
<i class="no-highlight">95</i>&nbsp;    }
<i class="no-highlight">96</i>&nbsp;
<i class="no-highlight">97</i>&nbsp;    /**
<i class="no-highlight">98</i>&nbsp;     * Renders a single square on the given graphics context on the specified
<i class="no-highlight">99</i>&nbsp;     * rectangle.
<i class="no-highlight">100</i>&nbsp;     *
<i class="no-highlight">101</i>&nbsp;     * @param square
<i class="no-highlight">102</i>&nbsp;     *            The square to render.
<i class="no-highlight">103</i>&nbsp;     * @param graphics
<i class="no-highlight">104</i>&nbsp;     *            The graphics context to draw on.
<i class="no-highlight">105</i>&nbsp;     * @param x
<i class="no-highlight">106</i>&nbsp;     *            The x position to start drawing.
<i class="no-highlight">107</i>&nbsp;     * @param y
<i class="no-highlight">108</i>&nbsp;     *            The y position to start drawing.
<i class="no-highlight">109</i>&nbsp;     * @param width
<i class="no-highlight">110</i>&nbsp;     *            The width of this square (in pixels.)
<i class="no-highlight">111</i>&nbsp;     * @param height
<i class="no-highlight">112</i>&nbsp;     *            The height of this square (in pixels.)
<i class="no-highlight">113</i>&nbsp;     */
<i class="no-highlight">114</i>&nbsp;    private void render(Square square, Graphics graphics, int x, int y, int width, int height) {
<b class="nc"><i class="no-highlight">115</i>&nbsp;        square.getSprite().draw(graphics, x, y, width, height);</b>
<b class="nc"><i class="no-highlight">116</i>&nbsp;        for (Unit unit : square.getOccupants()) {</b>
<b class="nc"><i class="no-highlight">117</i>&nbsp;            unit.getSprite().draw(graphics, x, y, width, height);</b>
<b class="nc"><i class="no-highlight">118</i>&nbsp;        }</b>
<i class="no-highlight">119</i>&nbsp;    }
<i class="no-highlight">120</i>&nbsp;}
</div>
</pre>
</div>

<script type="text/javascript">
(function() {
    var msie = false, msie9 = false;
    /*@cc_on
      msie = true;
      @if (@_jscript_version >= 9)
        msie9 = true;
      @end
    @*/

    if (!msie || msie && msie9) {
        var codeBlock = document.getElementById('sourceCode');

        if (codeBlock) {
            hljs.highlightBlock(codeBlock);
        }
    }
})();
</script>

<div class="footer">
    
    <div style="float:right;">generated on 2022-05-22 18:55</div>
</div>
</body>
</html>
